package Stragos.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Stragos.excepcion.DAOExcepcion;
import Stragos.modelo.Producto;
import Stragos.modelo.Tipoproducto;
import Stragos.negocio.GestionProductos;

/**
 * Servlet implementation class BuscarProd
 */
@WebServlet("/BuscarProd")
public class BuscarProd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuscarProd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try{
			GestionProductos prod = new GestionProductos();
			String nom  = request.getParameter("buscar");
			String prd  = request.getParameter("buscarprod");
			if (prd == null) {
			
			List<Producto> p1 = (ArrayList<Producto>) prod.buscarPorNombre(nom);
			//Producto p1 = (Producto)prod.buscarPorNombre(nom);
			request.setAttribute("busqueda",p1);
			RequestDispatcher rd = request.getRequestDispatcher("MatProd.jsp");
			rd.forward(request, response);
			} else{
				if(nom == null){
				List<Producto> p2 = (ArrayList<Producto>) prod.buscarPorNombre(prd);
				request.setAttribute("busquedaped",p2);
				RequestDispatcher rd1 = request.getRequestDispatcher("RealizarPedProd.jsp");
				rd1.forward(request, response);
				}
			}			
					
			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}
